package com.hgyc.mom.qms.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.qms.entity.QmsTemplate;
import com.hgyc.mom.qms.vo.QmsTemplateParam;
import com.hgyc.mom.qms.service.QmsTemplateService;
import com.hgyc.mom.qms.vo.QmsTemplateVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 检测模板Controller
 *
 * @author fwj
 * @since 2025-09-26
 */
@Tag(name = "检测模板控制器", description = "检测模板控制器")
@RestController
@RequestMapping("/qms/template")
public class QmsTemplateController {
    @Autowired
    private QmsTemplateService qmsTemplateService;

    @Operation(summary = "查询检测模板列表", description = "查询检测模板列表")
    @PostMapping("/pageList")
    public Result<IPage<QmsTemplate>> pageList(@Parameter(description = "查询参数") @RequestBody QmsTemplateParam qmsTemplateParam) {
        Page<QmsTemplate> page = new Page<>(qmsTemplateParam.getPage(), qmsTemplateParam.getPageSize());
        QmsTemplate qmsTemplate = new QmsTemplate();
        BeanUtils.copyProperties(qmsTemplateParam, qmsTemplate);
        QueryWrapper<QmsTemplate> wrapper = QueryWrapperUtil.build(qmsTemplate);
        IPage<QmsTemplate> list = qmsTemplateService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取检测模板详细信息", description = "获取检测模板详细信息")
    @GetMapping(value = "/{id}")
    public Result<QmsTemplate> getInfo(@Parameter(description = "检测模板ID") @PathVariable("id") Long id) {
        return Result.success(qmsTemplateService.getById(id));
    }
    @Operation(summary = "新增检测模板", description = "新增检测模板")
    @PostMapping
    public Result<?> save(@Parameter(description = "检测模板") @RequestBody QmsTemplateVO qmsTemplateVO) {
        qmsTemplateService.saveTemplate(qmsTemplateVO);
        return Result.success();
    }

    @Operation(summary = "修改检测模板", description = "修改检测模板")
    @PutMapping
    public Result<?> update(@Parameter(description = "检测模板") @RequestBody QmsTemplateVO qmsTemplateVO) {
        qmsTemplateService.updateTemplate(qmsTemplateVO);
        return Result.success();
    }

    @Operation(summary = "删除检测模板", description = "删除检测模板")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        qmsTemplateService.removeTemplate(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<QmsTemplate> util = new ExcelUtil<>(QmsTemplate.class);
        util.importTemplateExcel(response, "检测模板");
    }

    @Operation(summary = "导出检测模板列表", description = "导出检测模板列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsTemplateParam qmsTemplateParam) {
        QmsTemplate qmsTemplate = new QmsTemplate();
        BeanUtils.copyProperties(qmsTemplateParam, qmsTemplate);
        QueryWrapper<QmsTemplate> wrapper = QueryWrapperUtil.build(qmsTemplate);
        List<QmsTemplate> list = qmsTemplateService.list(wrapper);
        ExcelUtil<QmsTemplate> util = new ExcelUtil<>(QmsTemplate.class);
        util.exportExcel(response, list, "检测模板数据");
    }

    @Operation(summary = "导入检测模板列表", description = "导入检测模板列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<QmsTemplate> util = new ExcelUtil<>(QmsTemplate.class);
        List<QmsTemplate> qmsTemplateList = util.importExcel(file.getInputStream());
        String message = qmsTemplateService.importData(qmsTemplateList, updateSupport);
        return Result.success(message);
    }
}
